package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaishengit.entity.ContactNode;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class ContactNodeDao {

	public DBHelp<ContactNode> db = new DBHelp<ContactNode>();
	
	public void save(ContactNode contactNode){
		String sql = "insert t_contact_node(id,content,createtime,contactId,userId) values (?,?,?,?,?)";
		db.executeSQL(sql, contactNode.getId(),contactNode.getContent(),contactNode.getCreateTime(),contactNode.getContactId(),contactNode.getUserId());
	}
	
	public void update(ContactNode contactNode){
		String sql = "update t_contact_node set content = ?, createtime = ?, contactId = ?, userId = ? where id = ?";
		db.executeSQL(sql, contactNode.getContent(),contactNode.getCreateTime(),contactNode.getContactId(),contactNode.getUserId(),contactNode.getId());
	}
	
	public void del(int id){
		String sql = "delete from t_document where id = ?";
		db.executeSQL(sql, id);
		
	}
	
	public ContactNode findById(int id){
		String sql = "select id,content,createtime,contactId,userId from t_contact_node where id = ?";
		return db.executeQueryToObject(sql, new ContactNodeRowMapper(), id);
	}
	
	private class ContactNodeRowMapper implements RowMapper<ContactNode>{

		public ContactNode mapRow(ResultSet rs) throws SQLException {
			ContactNode contactNode = new ContactNode();
			contactNode.setId(rs.getInt("id"));
			contactNode.setContactId(rs.getInt("contactId"));
			contactNode.setUserId(rs.getInt("userId"));
			contactNode.setContent(rs.getString("content"));
			contactNode.setCreateTime(rs.getString("createtime"));
			return contactNode;
		}
		
	}
	
}
